//
//  NSString+Extention.m
//  FanBei
//
//  Created by 哀木涕 on 2016/12/26.
//  Copyright © 2016年 chuxiangkeji. All rights reserved.
//

#import "NSString+Extention.h"
#import <CommonCrypto/CommonDigest.h>

//手机号码验证13开头
#define isPhone @"^((1[34578][0-9])|(14[57])|(17[0678]))\\d{8}$"
//邮箱验证
#define isMail @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"
//身份证验证
#define isIDCard @"^\\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$"
//密码验证
#define isPassword @"^(?![^a-zA-Z]+$)(?!\\D+$).{8,16}$"
//银行卡简单验证 16位和19位
#define isBankCard @"^\\d{16}|\\d{19}$"
//中文验证
#define isChinese @"^[\u4E00-\u9FA5]*$"
//验证字母和数字
#define isLetterOrNumber @"^[a-zA-Z0-9]{0,}$"
//验证纯数字
#define isNumber @"^[0-9]*$"





@implementation NSString (Extention)

@end


@implementation NSString (Times)

+(NSString *)GetNowTimes{
    
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[date timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f",a];
    NSLog(@"time is %@",timeString);
    return timeString;
    
}
+(NSString *)getTimeStr:(NSString *)getTime{
    
    NSLog(@"time is %ld",(long)[getTime integerValue]);
    NSTimeInterval time=[getTime doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}
+(NSString *) md5: (NSString *) inPutText
{
    const char *cStr = [inPutText UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    
    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];
}

+(NSString *)GetNowTimesWithMD5{
    
    
    NSString* timeWithMd5 = [self md5:[NSString stringWithFormat:@"%@time%@%@",APPKEY,[self GetNowTimes],APPKEY]];
    
    return timeWithMd5;
    
}

+(NSDictionary *)GetParamDic{
    
    NSDictionary *parameteiDic=@{@"sign":[NSString GetNowTimesWithMD5],@"time":[NSString GetNowTimes]};
    return parameteiDic;
    
}

+(NSString *)GetSignString:(NSArray *)oldArray{
    //排序后
    NSArray *newArray = [oldArray sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"old Array = %@,new array = %@",oldArray,newArray);
    //拼接
    NSString *sorted = [newArray componentsJoinedByString:@""];
    //生成sign
    NSString* sign = [NSString md5:[NSString stringWithFormat:@"%@%@%@",APPKEY,sorted,APPKEY]];
    NSLog(@"sign is%@",sign);
    
    return sign;
}

+ (NSString *)encodeToPercentEscapeString: (NSString *) input
{
    NSString *outputStr = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                       NULL, /* allocator */
                                                                                       (__bridge CFStringRef)input,
                                                                                       NULL, /* charactersToLeaveUnescaped */
                                                                                       (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    return
    outputStr;
}
@end


@implementation NSString (Validation)

- (BOOL)isType:(StringType)stringType
{
    return [self validationExpression:[self expressionByValidationType:stringType]];
}

- (BOOL)validationExpression:(NSString *)expression
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",expression];
    return [predicate evaluateWithObject:self];
}

/**
 *  根据类型返回对应的正则表达式
 *
 *  @param stringType 待验证的正则表达式
 *
 *  @return 正则表达式
 */
- (NSString *)expressionByValidationType:(StringType)stringType
{
    NSString *str;
    switch (stringType) {
        case StringTypePhone:
            str = isPhone;
            break;
        case StringTypeMail:
            str = isMail;
            break;
        case StringTypeIDCard:
            str = isIDCard;
            break;
        case StringTypePassword:
            str = isPassword;
            break;
        case StringTypeBankCard:
            str = isBankCard;
            break;
        case StringTypeChinese:
            str = isChinese;
            break;
        case StringTypeLetterOrNumber:
            str = isLetterOrNumber;
            break;
        case StringTypeNumber:
            str = isNumber;
            break;
        default:
            str = @"";
            break;
    }
    return str;
}

- (BOOL)isEmptyString{
    if (self == nil || self == NULL || self.length == 0) {
        return YES;
    }
    if ([self isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

@end




